package com.itheima.sevrice.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Category;
import com.itheima.common.PageParam;
import com.itheima.dao.CategoryDao;
import com.itheima.sevrice.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired(required = false)
    private CategoryDao categoryDao;

    /**
     * 分页查询
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage<Category> findPage(PageParam pageParam) {
        //构建分页条件对象
        IPage<Category> page=new Page<>(pageParam.getPage(),pageParam.getPageSize());

        //执行条件
        return categoryDao.selectPage(page,null);
    }

    /**
     * 根据菜品dishId 查询菜品分类
     *
     * @param dishId
     * @return
     */
    @Override
    public Category findByDishId(Long dishId) {
        return categoryDao.selectById(dishId);
    }

    /**
     * 根据套餐的SetmealId 查询套餐分类
     *
     * @param setmealId
     * @return
     */
    @Override
    public Category findBySetmealId(Long setmealId) {
        return categoryDao.selectById(setmealId);
    }

    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @Override
    public int addCategory(Category category) {
        return categoryDao.insert(category);
    }


    /**
     * 删除分类
     *
     * @param id
     * @return
     */
    @Override
    public int deleteCategory(Long id) {
        return categoryDao.deleteById(id);
    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @Override
    public int updateCategory(Category category) {
        return categoryDao.updateById(category);
    }

    /**
     * 根据type 来查询分类。菜品分类或 套餐分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findByType(Integer type) {
        //构建条件对象
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //追加条件
        lqw.eq(type !=null, Category::getType,type);
        //执行条件
        return categoryDao.selectList(lqw);
    }


}
